package com.ruoyi.basedata.controller;

import com.ruoyi.basedata.domain.GoodsType;
import com.ruoyi.basedata.service.IGoodsTypeService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 商品类别Controller
 *
 * @author wl
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/type")
@Slf4j
public class GoodsTypeController extends BaseController {
    @Autowired
    private IGoodsTypeService goodsTypeService;

    /**
     * 查询商品类别列表
     */
//    @RequiresPermissions("basedata:type:list:all")
    @GetMapping("/allList")
    public AjaxResult getAllList() {
        log.info("GoodsTypeController getAllList start...");
        AjaxResult ajaxResult = goodsTypeService.selectGoodsTypeAllList();
        log.info("GoodsTypeController getAllList ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 查询商品类别列表
     */
//    @RequiresPermissions("basedata:type:list")
    @GetMapping("/list")
    public TableDataInfo list(GoodsType goodsType) {
        log.info("GoodsTypeController list goodsType:{}", goodsType);
        TableDataInfo tableDataInfo = goodsTypeService.selectGoodsTypeList(goodsType);
        log.info("GoodsTypeController list tableDataInfo:{} goodsType:{}", tableDataInfo, goodsType);
        return tableDataInfo;
    }


    /**
     * 导出商品类别列表
     */
    @RequiresPermissions("basedata:type:export")
    @Log(title = "商品类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsType goodsType) {
        List<GoodsType> list = (List<GoodsType>) goodsTypeService.selectGoodsTypeList(goodsType).getRows();
        ExcelUtil<GoodsType> util = new ExcelUtil<GoodsType>(GoodsType.class);
        util.exportExcel(response, list, "商品类别数据");
    }

    /**
     * 获取商品类别详细信息
     */
    @RequiresPermissions("basedata:type:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        log.info("GoodsTypeController getInfo id:{}", id);
        AjaxResult ajaxResult = success(goodsTypeService.selectGoodsTypeById(id));
        log.info("GoodsTypeController getInfo ajaxResult:{} id:{}", ajaxResult, id);
        return ajaxResult;
    }

    /**
     * 新增商品类别
     */
    @RequiresPermissions("basedata:type:add")
    @Log(title = "商品类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsType goodsType) {
        log.info("GoodsTypeController add goodsType:{}", goodsType);
        AjaxResult ajaxResult = goodsTypeService.insertGoodsType(goodsType);
        log.info("GoodsTypeController add ajaxResult:{} goodsType:{}", ajaxResult, goodsType);
        return ajaxResult;
    }

    /**
     * 修改商品类别
     */
    @RequiresPermissions("basedata:type:edit")
    @Log(title = "商品类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsType goodsType) {
        log.info("GoodsTypeController edit goodsType:{}", goodsType);
        AjaxResult ajaxResult = goodsTypeService.updateGoodsType(goodsType);
        log.info("GoodsTypeController edit ajaxResult:{} goodsType:{}", ajaxResult, goodsType);
        return ajaxResult;
    }

    /**
     * 删除商品类别
     */
    @RequiresPermissions("basedata:type:remove")
    @Log(title = "商品类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("GoodsTypeController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = goodsTypeService.deleteGoodsTypeByIds(ids);
        log.info("GoodsTypeController remove ajaxResult:{} ids:{}", ajaxResult, Arrays.toString(ids));
        return ajaxResult;
    }

    /**
     * 校验商品类别编码是否唯一
     */
    @Log(title = "校验商品类别", businessType = BusinessType.OTHER)
    @GetMapping("/checkTypeCode")
    public AjaxResult checkTypeCode(String typeCode) {
        log.info("GoodsTypeController checkTypeCode typeCode:{}", typeCode);
        AjaxResult ajaxResult = goodsTypeService.checkGoodsTypeCode(typeCode);
        log.info("GoodsTypeController checkTypeCode ajaxResult:{} typeCode:{}", ajaxResult, typeCode);
        return ajaxResult;
    }
}
